LocalStrategy.validate   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 9
c 0
b 0
f 0
rs 9.95
cc 2
1
import { Strategy } from 'passport-local';
2
import { PassportStrategy } from '@nestjs/passport';
3
import { Injectable, UnauthorizedException, Inject } from '@nestjs/common';
4
import { AuthenticatedView } from 'src/Application/HumanResource/User/View/AuthenticatedView';
5
import { IQueryBus } from '@nestjs/cqrs';
6
import { LoginQuery } from 'src/Application/HumanResource/User/Query/LoginQuery';
7
8
@Injectable()
9
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
10
  constructor(
11
    @Inject('IQueryBus')
12
    private readonly queryBus: IQueryBus
13
  ) {
14
    super({
15
      usernameField: 'email'
16
    });
17
  }
18
19
  public async validate(
20
    email: string,
21
    password: string
22
  ): Promise<AuthenticatedView> {
23
    try {
24
      return await this.queryBus.execute(new LoginQuery(email, password));
25
    } catch (err) {
26
      throw new UnauthorizedException('login-error-failed');
27
    }
28
  }
29
}
30